# Figure 4

library(tidyverse)

DATA        <- read.csv(file.choose(), header=TRUE)

DATA$FACET  <- factor(DATA$FACET, levels=c("Trump", "McConnell", "Ryan", "Republicans"))
DATA$LEVEL  <- factor(DATA$LEVEL, levels=0:10)

LABELS      <- c("Republicans"="Republicans\nin 2017", "McConnell"="Mitch McConnell\nin 2018", "Ryan"="Paul Ryan\nin 2018", "Trump"="Donald Trump\nin 2018")

theme.z <- theme(
        axis.text.x        = element_text(size=15, color="black", hjust=c(0,1), margin=margin(t=7,b=7)),
        axis.text.x.top    = element_blank(),
        axis.text.y        = element_text(size=15, color="black",               margin=margin(l=7,r=7)),
        axis.text.y.right  = element_text(size=15, color="black",               margin=margin(l=7,r=7)),
        axis.ticks.x       = element_blank(),
        axis.ticks.y       = element_blank(),
        axis.title.x       = element_text(size=18, color="black",               margin=margin(t=7,r=7,b=7,l=7)),
        axis.title.y       = element_blank(),
        legend.position    = "none",
        panel.background   = element_rect(size=0.5, color="black", fill="gray90", linetype="solid"),
        panel.border       = element_rect(size=1.8, color="black", fill=NA, linetype="solid"),
        panel.grid.major.x = element_blank(), 
        panel.grid.major.y = element_blank(),
        panel.grid.minor.x = element_blank(), 
        panel.grid.minor.y = element_blank(),
        panel.spacing.x    = unit(1, "lines"),
        panel.spacing.y    = unit(1, "lines"),
        plot.background    = element_rect(fill="white"),
        plot.caption       = element_text(size=12, hjust=0  , margin=margin(t=10)),
        plot.margin        = unit(c(t=5,r=5,b=5,l=5),"pt"),
        plot.subtitle      = element_text(size=15, hjust=0.5, margin=margin(b=7)),
        plot.title         = element_text(size=20, hjust=0.5, face="bold", margin=margin(t=0,b=12)),
        strip.background   = element_rect(color="black", fill="black"),
        strip.text.x       = element_text(size=18, color="white", face="bold", margin=margin(t=10,r=10,b=10,l=10)))

ggplot(data=DATA, aes(x=LEVEL, y=PE, group=1)) +
    facet_wrap(vars(FACET), ncol=4, dir="v", labeller=as_labeller(LABELS)) +
    #geom_rect(data=filter(DATA, FACET=="Ryan"), aes(xmin=-Inf, xmax=Inf, ymin=min(DATA$PE[DATA$FACET=="Ryan"]), ymax=max(DATA$PE[DATA$FACET=="Ryan"])), fill="lightsteelblue3", inherit.aes=FALSE) +
    #geom_rect(data=filter(DATA, FACET=="McConnell"), aes(xmin=-Inf, xmax=Inf, ymin=min(DATA$PE[DATA$FACET=="McConnell"]), ymax=max(DATA$PE[DATA$FACET=="McConnell"])), fill="lightsteelblue3", inherit.aes=FALSE) +
    #geom_rect(data=filter(DATA, FACET=="Republicans"), aes(xmin=-Inf, xmax=Inf, ymin=min(DATA$PE[DATA$FACET=="Republicans"]), ymax=max(DATA$PE[DATA$FACET=="Republicans"])), fill="lightsteelblue3", inherit.aes=FALSE) +
    #geom_rect(data=filter(DATA, FACET=="Trump"), aes(xmin=-Inf, xmax=Inf, ymin=min(DATA$PE[DATA$FACET=="Trump"]), ymax=max(DATA$PE[DATA$FACET=="Trump"])), fill="lightsteelblue3", inherit.aes=FALSE) +
    geom_line(size=1.25, color="red4") +
    geom_text(data=filter(DATA, FACET=="Ryan"), aes(x=9.5, y=0.9, label="+0.03"), color="red4", hjust=1, vjust=1, size=5.5) +
    geom_text(data=filter(DATA, FACET=="McConnell"), aes(x=9.5, y=0.9, label="-0.01"), color="red4", hjust=1, vjust=1, size=5.5) +
    geom_text(data=filter(DATA, FACET=="Republicans"), aes(x=9.5, y=0.9, label="+0.04"), color="red4", hjust=1, vjust=1, size=5.5) +
    geom_text(data=filter(DATA, FACET=="Trump"), aes(x=9.5, y=0.9, label="+0.26"), color="red4", hjust=1, vjust=1, size=5.5) +
    scale_x_discrete(name="Animus toward Democratic groups in 2011", expand=c(0,0), breaks=c(1,10), labels=c("Low\nanimus","High\nanimus")) +
    scale_y_continuous(expand=c(0,0), limits=c(0,1), breaks=seq(0,1,0.25), labels=scales::number_format(accuracy=0.01), sec.axis=dup_axis()) +
  labs(title="Predicted probability of a favorable rating about...") +
    theme.z

ggsave(file="Figure 4.wmf", width=12, height=4.5)
getwd()
